<?php

class StatisticSiteController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules(){
	    return array(
	        array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
	        array('deny', // deny anything else
	              'users'=>array('*'),
	        ),
	    );
	}

    public function actionIndex()
    {   
            $this -> render('index',array(	

            ));
    }
    
    public function actionCreate()
    {   
        if(Yii::app()->request->isPostRequest)
        {   
            $model = new StatisticSite();
            $data = $_POST['model'];
            $model -> setData($data);
            
            if($model -> startDate == "") $model -> startDate = null;
            if($model -> endDate == "") $model -> endDate = null;
            
            $model ->save();
            
            $model -> updateLifeTime($model -> id);
            
            $this->redirect("/statisticSite/index");
        }
        else
        {
            $this -> render('create',array(	
            ));
        }
    }
    
    public function actionEdit()
    {   
        if(Yii::app()->request->isPostRequest)
        {   
            $id = $_POST['id'];
            $model = StatisticSite::model() -> findByPk($id);

            $data = $_POST['model'];
            unset($data['id']);
            $model -> setData($data);
            
            if($model -> startDate == "") $model -> startDate = null;
            if($model -> endDate == "") $model -> endDate = null;
            
            $model ->save();
            
            $model -> updateLifeTime($model -> id);
            
            $this->redirect("/statisticSite/index");
        }
        else
        {
            $id = $_GET['id'];
            $model = StatisticSite::model() -> findByPk($id);
            
            $this -> render('edit',array(
                "model" => $model
            ));
        }
    }
    
    public function actionSearch()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $model = new StatisticSite();

            $data = $model -> search();

            echo json_encode(array("results" => $data[0], "total" => $data[1]));
            return;
        }
    }

    public function actionDelete()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $id = $_POST['id'];			
            $item = StatisticSite::model() -> findByPk($id);
            if ($item -> delete()) {
                 echo json_encode(array("status" => 'success'));
             }
             else{
                echo json_encode(array("status" => 'failure'));
             }					
        }
        
        return;
    }
    
}